package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckItemService checkItemService;

    //检查组录入,中间表数据添加,修改检查组和检查项
    @Override
    public Boolean saveOrUpdateCheckGroupInfo(CheckGroupDTO checkGroupDTO) {
        //当进入此方法,有两种情况,一种是进行添加,一种是进行修改

        //1.当进行添加时,先对表单中的检查组进行录入保存,再对此检查组对应的检查项进行添加

        //2.当进行修改时,先对表单中被修改过的检查组进行修改,再根据检查项的id删除对应中间表的信息并重新添加

        // 先获取前端传入的检查组DTO对象的ID
        Integer id = checkGroupDTO.getId();

        //如果有ID,则会进行修改操作,修改之前先删除中间表中此检查组ID对应的检查项信息
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            //根据检查组的ID删除中间表数据
            baseMapper.deleteCheckitemAndCheckgroupInfoByGroupId(id);
        }
        //执行检查组以及相关检查项的信息的添加或修改
        boolean flag = saveOrUpdate(checkGroupDTO);

        //当完成录入,自动生成主键(这是t_checkgroup的ID)
        Integer gid = checkGroupDTO.getId();
        //中间表数据添加插入
        //1.拿到存储到对应检查组的检查项的ID数组
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        //2.遍历添加到此检查组ID对应的检查项信息
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                baseMapper.addCheckGroupIdAndCheckItemId(gid, checkitemId);
            }
        }
        return flag;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //   select  * from xxx where is_delete = 0  and  xxx like ?
        //  条件构造出来   Wrapper对象
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<CheckGroup>();
        //  条件,where后面的数据
        queryWrapper.eq("is_delete",0);//   where is_delete = 0
        Page<CheckGroup> page = null;
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }else{
            queryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            //   sql   + 追加  sql  where  name like  %?%
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }

        PageResult result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    @Override
    public Boolean deleteCheckgroupById(Integer id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);//逻辑删除
        return updateById(checkGroup);//调用mp自己的方法
    }

    @Override
    public CheckGroupVO findCheckitemInfoByGroupId(Integer id) {
        //查询所有检查项信息
        List<CheckItem> checkItemList = checkItemService.list();

        //根据gid查询对应的中间表的信息
        Integer[] checkitemIds = baseMapper.findCheckitemInfoByGroupId(id);
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(checkItemList);
        checkGroupVO.setCheckitemIds(checkitemIds);
        return checkGroupVO;
    }

}
